package com.yun.many.spec.http.exception;

import com.yun.many.spec.enums.ResultCode;
import com.yun.many.spec.exception.BusinessException;
import com.yun.many.spec.vo.HttpResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@ControllerAdvice(annotations = {RestController.class})
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public HttpResult<Void> exceptionHandler(Exception e) {
        log.error("接口异常", e);
        return HttpResult.failure(ResultCode.SYS_EXCEPTION);
    }

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public HttpResult<Void> businessExceptionHandler(BusinessException e) {
        String code = e.getCode();
        String msg = e.getMessage();
        return HttpResult.failure(code, msg);
    }
}
